package com.evil.mi.service.impl;

import com.evil.mi.bean.VideoBean;
import com.evil.mi.bean.VideoDetailBean;
import com.evil.mi.bean.VideoImgBean;
import com.evil.mi.crawler.VideoDetailCrawlerComponent;
import com.evil.mi.crawler.VideoListCrawlerComponent;
import com.evil.mi.entity.VideoEntity;
import com.evil.mi.entity.VideoImgEntity;
import com.evil.mi.service.CrawlerService;
import com.evil.mi.service.VideoImgService;
import com.evil.mi.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
 * com.evil.mi.service.impl
 *
 * @author 白雨浓
 * date: 2018/8/25 13:06
 * content:
 */
@Service
@Slf4j
public class CrawlerServiceImpl implements CrawlerService {

    private static final AtomicBoolean start = new AtomicBoolean(false);

    @Autowired
    private VideoListCrawlerComponent videoListCrawlerComponent;

    @Autowired
    private VideoDetailCrawlerComponent videoDetailCrawlerComponent;

    @Autowired
    private VideoService videoService;

    @Autowired
    private VideoImgService videoImgService;

    @Override
    public void stop() {
        start.set(false);
    }

    @Override
    public boolean getStatus() {
        return start.get();
    }

    @Override
    @Async
    public void start() {
        start.set(true);
        //53
        for (int i = 0; i < 500; i++) {
            if (!start.get()) {
                break;
            }
            try {
                handleList(i);
                Thread.sleep(new Random().nextInt(5) * 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void handleList(int current) {
        List<String> hrefList = videoListCrawlerComponent.getListDetail(current);

        if (hrefList.size() == 0) {
            this.stop();
        }

        for (String herf : hrefList) {
            try {
                handleDetail(herf);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private void handleDetail(String url) {
        VideoDetailBean detailBean = videoDetailCrawlerComponent.getVideoDetail(url);

        log.info("开始爬取>> url:{}", url);
        if (detailBean == null) {
            log.warn("内容为空>> url:{}", url);
            return;
        }

        VideoBean videoBean = detailBean.getVideo();
        VideoEntity videoEntity = new VideoEntity();
        BeanUtils.copyProperties(videoBean, videoEntity);
        videoEntity.setUrl(url);
        Integer videoId = videoService.saveVideo(videoEntity);

        List<VideoImgBean> videoImgBeanList = detailBean.getVideoImgList();

        List<VideoImgEntity> videoImgEntityList = videoImgBeanList.stream().map(bean -> {
            VideoImgEntity entity = new VideoImgEntity();
            BeanUtils.copyProperties(bean, entity);
            return entity;
        }).collect(Collectors.toList());


        videoImgEntityList.forEach(entity -> {
            entity.setVideoId(videoId);
            videoImgService.saveVideoImg(entity);
        });
    }

}
